package com.example.myapplication.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.myapplication.BaseFgm;
import com.example.myapplication.R;
import com.example.myapplication.activity.GetDetailAty;
import com.example.myapplication.adp.RecycleListAdp;
import com.example.myapplication.database.GetItem;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class RecycleFgm extends BaseFgm {
    private ArrayList<GetItem> datas=new ArrayList<GetItem>();
    private RecycleListAdp adp;
    private RecyclerView recyclerView;

    @Override
    protected void logicStart() {
        datas.clear();
        List<GetItem> all = LitePal.where("isFinish = 'false'").find(GetItem.class);

        datas.addAll(all);
        adp.notifyDataSetChanged();


    }

    @Override
    protected void initView() {

        recyclerView=rootView.findViewById(R.id.listView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false));
        adp=new RecycleListAdp(datas);
        recyclerView.setAdapter(adp);
        adp.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.dv_12dp,null));


    }

    @Override
    protected void setListener() {
        adp.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                Intent intent = new Intent(getContext(), GetDetailAty.class);
                intent.putExtra("id",datas.get(position).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void refreshState() {
        super.refreshState();
        datas.clear();
        List<GetItem> all = LitePal.where("isFinish = 'false'").find(GetItem.class);
        datas.addAll(all);
        adp.notifyDataSetChanged();
    }

    @Override
    protected int getlayout() {
        return R.layout.lo_list;
    }
}
